﻿/*
 * Created by Wang Lei on 21-07-01.
 * Copyright (c) 2006-2021 Kaitiren
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

using UnityEngine;
using System.Collections;

namespace HLLib
{
	/// <summary>
	/// FPS显示部分.
	/// HLShowFPS
	/// </summary>
	public class HLShowFPSListener : MonoBehaviour
	{

		/// <summary>
		/// 每次刷新计算的时间: 帧/秒
		/// </summary>
		public float updateInterval = 0.5f;

		/// <summary>
		/// 最后间隔结束时间
		/// </summary>
		private double lastInterval;
		private int frames = 0;
		private float currFPS;
		private float minFPS = float.MaxValue;
		private float maxFPS = float.MinValue;

		string[] data = new string[4];

		private bool IsShowButton = false;
		// Use this for initialization
		void Start()
		{
			lastInterval = Time.realtimeSinceStartup;
			frames = 0;

			data[0] = SystemInfo.deviceName;
			data[1] = SystemInfo.operatingSystem;
			data[2] = SystemInfo.processorType;
			data[3] = SystemInfo.processorCount.ToString();
		}

		// Update is called once per frame
		void Update()
		{
			++frames;
			float timeNow = Time.realtimeSinceStartup;
			if (timeNow > lastInterval + updateInterval)
			{
				currFPS = (float)(frames / (timeNow - lastInterval));
				if (currFPS < minFPS)
					minFPS = currFPS;
				if (currFPS > maxFPS)
					maxFPS = currFPS;
				frames = 0;
				lastInterval = timeNow;
			}
			if (Input.GetMouseButtonDown(0) 
				&& Input.mousePosition.x < Screen.width * 0.05f && Input.mousePosition.y > Screen.height * 0.95f)
			{
				IsShowButton = !IsShowButton;
			}
		}

		private void OnGUI()
		{
			GUIStyle bb = new GUIStyle();
			bb.normal.background = null;
			bb.normal.textColor = new Color(1, 0, 0);
			bb.fontSize = 25;

			GUILayout.Box("CurFPS:" + currFPS.ToString("f2"), bb);
			GUILayout.Box("CPU:" + data[2], bb);
			
			//
			// GUILayout.Box("MinFPS:" + minFPS.ToString("f2"));
			// GUILayout.Box("MaxFPS:" + maxFPS.ToString("f2"));
			// GUILayout.Box("Name:" + data[0
			// GUILayout.Box("System:" + data[1]);
			//GUILayout.Box("CPUCount:" + data[3]);
		}
	}

}
